package com.xpsoft.oa.service.project.impl;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.reflect.TypeToken;
import com.xpsoft.core.service.impl.BaseServiceImpl;
import com.xpsoft.core.util.JsonUtil;
import com.xpsoft.oa.dao.project.ProjectReceiveDao;
import com.xpsoft.oa.dao.project.ProjectReceiveInfoDao;
import com.xpsoft.oa.model.project.ProjectReceive;
import com.xpsoft.oa.model.project.ProjectReceiveInfo;
import com.xpsoft.oa.service.project.ProjectReceiveService;

public class ProjectReceiveServiceImpl extends BaseServiceImpl<ProjectReceive> implements ProjectReceiveService{
	private ProjectReceiveDao dao;
	@Autowired
	private ProjectReceiveInfoDao projectReceiveInfoDao;
	public ProjectReceiveServiceImpl(ProjectReceiveDao dao) {
		super(dao);
		this.dao=dao;
	}

	@Override
	public void saveProjectReceive(ProjectReceive projectReceive,
			String regInfoDataList) {
		// TODO Auto-generated method stub
		dao.save(projectReceive);
		Boolean isNew = true;//新增标志
		if(projectReceive.getId()!=null){
			isNew = false;
		}
		dao.save(projectReceive);
		if(StringUtils.isNotBlank(regInfoDataList)){
			List<ProjectReceiveInfo> projectReceiveInfoList = JsonUtil.fromJsonTypesWithDate(regInfoDataList,
					new TypeToken<List<ProjectReceiveInfo>>() {
					}.getType());
			if(!isNew){//修改
				List<Long> idList = new ArrayList<Long>();
				for(ProjectReceiveInfo projectReceiveInfo:projectReceiveInfoList){
					if(projectReceiveInfo.getId()!=null){//修改的记录
						idList.add(projectReceiveInfo.getId());
					}
				}
				if(idList.size()>0){//先进行删除
					projectReceiveInfoDao.deleteProjectReceiveInfos(projectReceive.getId(), idList);
				}
			}
			for(ProjectReceiveInfo projectReceiveInfo:projectReceiveInfoList){
				projectReceiveInfo.setProjectReceive(projectReceive);
				projectReceiveInfoDao.save(projectReceiveInfo);
			}
		}
	}

}